//
//  YTMainTabBarController.swift
//  SinaProject
//
//  Created by 吴学宇 on 16/4/7.
//  Copyright © 2016年 吴学宇. All rights reserved.
//

import UIKit

class YTMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor();
        
        self.tabBar.tintColor = UIColor.orangeColor();

        self.addControllers();
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated);
        
        let width = UIScreen.mainScreen().bounds.size.width / CGFloat(viewControllers!.count);
        
        let rect = CGRectMake(0, 0, width, tabBar.frame.size.height);
        
        button.frame = CGRectOffset(rect, 2 * width, 0);
        
        tabBar.addSubview(button)
    }
    


    func addControllers() -> Void {
        
        
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil);
        
        if let pathSource = path  {
            
            let data = NSData(contentsOfFile: pathSource);
            
            do{
                
                let dicArr = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers);
                
                for var dic in dicArr as![[String : String ]] {
                    
                    [addChildViewController(dic["vcName"]!, title: dic["title"]!, imageName: dic["imageName"]!)];
                    
                }
                
                
            }catch{
                
                [addChildViewController("HomeViewController", title:"首页", imageName: "")]
                [addChildViewController("DiscoverViewController", title:"发现", imageName: "")]
                
                [addChildViewController("NullViewController", title:"发现", imageName: "")]
                [addChildViewController("MessageViewController", title:"消息", imageName: "")]
                
                [addChildViewController("MineViewController", title:"我的", imageName: "")]
                
            }
            
        }

        
    }
    

    func addChildViewController(childControllerName : String , title :String,imageName : String ) {
        
        let ns:String = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String;

        
        let controller = NSClassFromString(ns + "." + childControllerName) as! UIViewController.Type;
    
        let vc = controller.init();

        vc.tabBarItem = UITabBarItem(title: title, image: UIImage(named:imageName), selectedImage: UIImage(named: imageName));
        
        let nav = UINavigationController();
        
        nav.addChildViewController(vc);
        
        addChildViewController(nav);
        
        
    }
    
    func composeBtnClick(button:UIButton) -> Void {
        
        
    }
    
    
    private lazy var button : UIButton = {
        
        var btn = UIButton(type: UIButtonType.Custom);
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal);
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Selected);
   
        // 3.设置背景图片
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    
        // 4.添加监听
        btn.addTarget(self, action: "composeBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn;
        
        
    }();
    
    

    
    

  
}
